from keras.models import Sequential
from keras.layers import Dense
from sklearn.model_selection import train_test_split
from sklearn import datasets
from sklearn.model_selection import  KFold
from sklearn.model_selection import  cross_val_score
from keras.wrappers.scikit_learn import KerasClassifier

import  numpy as np

def create_model(optimizer='adam',init='glorot_uniform'):
    model = Sequential()
    model.add(Dense(4,input_dim=4,activation='relu'))
    model.add(Dense(6,activation='relu'))
    model.add(Dense(3,activation='softmax',kernel_initializer=init))

    model.compile(loss='categorical_crossentropy',optimizer=optimizer,metrics=['accuracy'])
    return model


seed = 7
np.random.seed(seed)
data = datasets.load_iris()
x = data.data
y = data.target

# model = KerasClassifier(build_fn=create_model)
# kfold = KFold(n_splits=10,shuffle=True,random_state=seed)
# results = cross_val_score(model,x,y,cv=kfold)
# print(results.mean()*100)

# dataset = np.loadtxt('pima-indians-diabetes.csv',delimiter=',')
# x = dataset[:,0:8]
# y = dataset[:,8]
#
# x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.2,random_state=seed)
#
model = create_model()

model.fit(x=x,y=y,epochs=100,batch_size=20)
socre = model.evaluate(x,y)
# # verbose = 0 为不在标准输出流输出日志信息
# # verbose = 1 为输出进度条记录
# # verbose = 2 为每个epoch输出一行记录
# print("%s : %.2f%%" %(model.metrics_names[1],socre[0]*100))
model.summary()
# model.save("mytest.h5")
#
# print(x.shape,y.shape)